<template>
  <div class="rated">
    <p class="title">最受好评电影</p>
    <div class="wrapper">
      <ul>
        <li v-for="item in ratedList" :key="item._id">
          <div class="imgwrap">
            <img :src="item.imgUrl" alt="" />
            <div class="score" v-if="item.score">
              观众评分：{{ item.score }}
            </div>
            <div class="score" v-else>{{ item.wishNum }} 人想看</div>
          </div>
          <p class="name">{{ item.title }}</p>
        </li>
      </ul>
    </div>
  </div>
</template>

<script>
// 哪个组件需要滚动就在哪个组件引入better-scroll
import BScroll from "better-scroll";
// import { nextTick } from "vue";
import { mapState, mapActions } from "vuex";

export default {
  // computed: mapState(["ratedList"]),
  computed: mapState({
    ratedList: (state) => state.index.ratedList,
  }),
  async mounted() {
    await this.asyncGetRatedList();
    new BScroll(".wrapper", {
      scrollX: true,
      scrollY: false,
      // 让原先的点击事件有效
      click: true,
    });
    // 添加bs的时机应当是asyncGetRatedList函数执行完成以后
  },
  methods: {
    // mapActions的时候，可以在前面额外添加一个参数，是模块名称
    ...mapActions("index", ["asyncGetRatedList"]),
    // async getData() {
    //   const res = await ratedApi();
    //   this.ratedList = res.result;

    //   await nextTick();
    //   // 添加滚动(必须要等待页面渲染完再加)
    //   // 第一个参数是容器的类名
    //   // 第二个参数是配置项对象
    //   new BScroll(".wrapper", {
    //     scrollX: true,
    //     scrollY: false,
    //     // 让原先的点击事件有效
    //     click: true,
    //   });
    // },
  },
};
</script>

<style lang="less" scoped>
.rated {
  width: 100%;
  height: 205px;
  margin-bottom: 10px;
  padding: 12px 15px;
  background-color: #fff;

  .title {
    font-size: 14px;
    color: #333;
    margin: 0 0 12px;
  }

  .wrapper {
    // 直接给容器加移出隐藏
    overflow: hidden;

    ul {
      height: 150px;
      // 将元素转成有弹性盒性质的行内元素
      display: inline-flex;
      // overflow: auto;

      li {
        width: 85px;
        height: 100%;
        margin-right: 10px;
        flex-shrink: 0;

        .imgwrap {
          width: 100%;
          height: 115px;
          position: relative;

          img {
            width: 100%;
            height: 100%;
          }

          .score {
            position: absolute;
            left: 0;
            bottom: 0;
            width: 100%;
            height: 24px;
            background: linear-gradient(
              to bottom,
              rgba(0, 0, 0, 0.3),
              rgba(0, 0, 0, 0.8)
            );
            color: #faaf00;
            line-height: 24px;
            font-size: @xxs-font;
          }
        }

        .name {
          font-size: @s-font;
          margin-top: 5px;
          font-weight: 900;
          .ell();
        }
      }
    }
  }
}
</style>
